package com.sdy.supermarket.web.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sdy.common.model.BizException;
import com.sdy.common.model.Response;
import com.sdy.common.utils.Assert;
import com.sdy.supermarket.biz.model.GsNavigation;
import com.sdy.supermarket.biz.service.GsNavigationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.sdy.mvc.controller.BaseController;

import java.util.HashMap;
import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 韩亚恒
 * @since 2019-07-24
 */
@Slf4j
@RestController
@RequestMapping("/gsNavigation")
public class GsNavigationController extends BaseController {
    @Autowired
    private GsNavigationService gsNavigationService;


    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping("/getNavigationList")
    public Response getNavigationList() {
        List<GsNavigation> list = gsNavigationService.queryAll();
        return Response.success(list);
    }

    /**
     * 首页获取所有导航
     *
     * @return
     */
    @GetMapping("/getShowList")
    public Response getShowList() {
        List<GsNavigation> showList = gsNavigationService.list(Wrappers.<GsNavigation>lambdaQuery().eq(GsNavigation::getDisplayStatus, 0).orderByAsc(GsNavigation::getSort));
        return Response.success(showList);
    }


    /**
     * 根据ID删除数据
     *
     * @param map id
     * @return
     */
    @PostMapping("/deleteById")
    public Response deleteNavigationById(@RequestBody HashMap<String, Object> map) throws BizException {
        Assert.isNull((Integer) map.get("id"), "ID为null");
        int i = gsNavigationService.deleteNavigation((Integer) map.get("id"));
        return i != 0 ? Response.success("删除成功") : Response.error("删除失败");
    }

    /**
     * 根据ID查询单条数据
     *
     * @return
     */
    @GetMapping("/getById")
    public Response getNavigationById(Integer id) {
        GsNavigation byId = gsNavigationService.getById(id);
        return Response.success(byId);
    }

    /**
     * 修改&新增
     *
     * @param gsNavigation
     * @return
     */
    @PostMapping("/saveOrUpdateNav")
    public Response updateNavigation(@RequestBody GsNavigation gsNavigation) throws BizException {
        int i = gsNavigationService.saveOrUpdateNavigation(gsNavigation);
        return i != 0 ? Response.success("操作成功") : Response.error("操作失败");
    }

    /**
     * 排序等级提升或降低
     *
     * @param map id type 0提升等级 1降低等级
     * @return
     */
    @PostMapping("/levelUpOrDown")
    public Response levelUpOrDown(@RequestBody HashMap<String, Object> map) throws BizException {
        int i = gsNavigationService.levelUpOrDown((Integer) map.get("id"), (Integer) map.get("type"));
        return i != 0 ? Response.success("修改成功") : Response.error("修改失败");
    }
}
